<?php
//TuấnNK add (20080617_10h):
//Các xử lý cho document:
//echo basename($_SERVER['PHP_SELF']);

require_once 'core/Debug.php'; //System Debug...
require_once 'core/config.php'; //System Config...
require_once 'core/Init.php';  //System Init...
//System process & output for ajax request

global $display;

$display->add('web_root',WEB_ROOT);
$display->add('logo',WEB_ROOT);

$display->add('date',date("d-m-Y",TIME_NOW));
$display->add('time',date("H\h i",TIME_NOW)." phút");

$all_items 	= array();

foreach (CGlobal::$subCategories as $topid => $subcats)
{
	if (isset(CGlobal::$allCategories[$topid]) && CGlobal::$allCategories[$topid]['status'] != "HIDE")
	{
		$all_items[$topid] = array(
									'name' 	=> CGlobal::$allCategories[$topid]['name'],
									'url' 	=> WEB_ROOT . "c$topid/".CGlobal::$allCategories[$topid]['ebname'],
									'subs'	=> array()
									);
									
		foreach($subcats as $sid => $scat)
		{
			if (isset(CGlobal::$allCategories[$sid]) && CGlobal::$allCategories[$sid]['status'] != "HIDE")
			{
				$all_items[$topid]['subs'][$sid] = array(
														'name' 	=> CGlobal::$allCategories[$sid]['name'],
														'url' 	=> WEB_ROOT . "c$sid/".CGlobal::$allCategories[$sid]['nice_name'],
														'items'	=> array()
														);
			}
		}
   }
}

$all_check_items = $all_items;

$last_seccid = 0;
	
$re 		= DB::query('SELECT * FROM item WHERE status = 1 ORDER BY level_1_catid, category_id, item_order');

$url = str_replace(WEB_DIR,'',WEB_ROOT);

if($re)
{
	while ($item = mysql_fetch_assoc($re))
	{
		$topcid = $item['level_1_catid'];
		$seccid = $item['category_id'];
		
		if(isset($all_items[$topcid]['subs'][$seccid]))
		{
			if($item['quantity'])
			{
				$item['quantity'] = "Có hàng";
			}
			else 
			{
				$item['quantity'] = "<font color=red>Call</font>";
			}
			
			if($item['price'])
			{
				if($item['currency_id'] == 2)
				{
					$item['price'] = $item['price'] * CGlobal::$exchange;
				}
				
				$item['price'] = Url::currency_format($item['price']);
			}
			else 
			{
				$item['price'] = "Liên hệ";
			}
			
			if ($item['img_url']){
				$item['img_url'] 		= $url . AZLib::getImageThumb($item['img_url'],80,80,0,$item['img_server']);
			}
			else{
				$item['img_url'] 		=  '';
			}
			
			$item['name'] 		= stripslashes($item['name']);
			$item['list_brief'] = stripslashes($item['list_brief']);
			$item['url'] 		= WEB_ROOT.CGlobal::$allCategories[$seccid]['nice_name']."/p{$item['id']}/".AZLib::safe_title($item['name']).".html";
			
			unset($all_check_items[$topcid]['subs'][$seccid]);
			
			$all_items[$topcid]['subs'][$seccid]['items'][$item['id']] = $item;
		}
	}
}

if($all_check_items)
{
	foreach ($all_check_items as $topid=>$topcat)
	{
		if($topcat['subs'])
		{
			foreach ($topcat['subs'] as $sid=>$scat)
			{
				if($all_check_items[$topid]['subs'][$sid])
				{
					unset($all_check_items[$topid]['subs'][$sid]);
				}
			}
			
			if($all_items[$topid]['subs'])
			{
				unset($all_items[$topid]);
			}
		}
	}
}

$display->add('all_items',$all_items);
$display->add('WEB_NAME',strtolower(WEB_NAME));

$file_name 		= WEB_NAME."_Bao_gia_".date("d.m.Y",TIME_NOW).".xls";
$file_content 	= $display->output('price_list',1,'Document');

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file_name");
header("Pragma: no-cache");
header("Expires: 0");

echo $file_content;

//End process & ouput
//Stop System
System::halt();
?>